package model.factories.environment;

import java.util.Map;
import java.util.Scanner;
import java.util.regex.Pattern;
import model.Interpreter;
import model.SyntaxErrorException;
import model.commands.Command;
import model.commands.misc.LiteralCommand;
import model.factories.CommandFactory;


/**
 * A factory used to build SetLanguageCommand objects.
 *
 * @author JLongley
 */
public class SetLanguageCommandFactory extends CommandFactory {
    
    private final Pattern mySetLanguagePattern = Pattern.
            compile(getInterpreter().getLanguage().getString("SetLanguageCommandPattern"));
    /**
     * 
     * @param interpreter an interpreter
     */
    public SetLanguageCommandFactory (Interpreter interpreter) {
        super(interpreter);
    }

    @Override
    public Command createCommand (Scanner commandReader,
                                  Map<String, Integer> variables) throws SyntaxErrorException {

        // remove command keyword;
        commandReader.next();

        String language = commandReader.next();
        getInterpreter().setLanguage(language);

        // return 1 for success
        return new LiteralCommand(1);
    }

    @Override
    public Pattern patternToMatch () {
        return mySetLanguagePattern;
    }
}
